The entire url is passed to the l() function which incorrectly encodes query strings, breaking the url. This can be resolved by changing the two occurrences of:

$html = l($display_term, $url, array('title' => "reference on $display_term", 'target' => $target));

to:

$url = explode('?', $url);
$html = l($display_term, $url[0], array('title' => "reference on $display_term", 'target' => $target, 'query' => $url[1]));