I have been playing around (modifying) with the relatedviews module attempting to filter a view based on the terms assigned to a node (mine being the users profile node) but I am having problems.

Would anyone like to have a look at this function and maybe spot out where I am going wrong or give me some tips..

I know the first part is working correctly which gets the currently logged in users profile nid.
I have added some commenting aswell =)

function relatedviews_handler_filter_taxcurnode(
    $op, $filter, $filterinfo, &$query) 
{
  global $user;
  $nid = NULL;
  $vid = NULL;
	//grabs the nid for the profile of the currently logged in user
	$result = db_query("SELECT nid FROM node WHERE type = 'uprofile' AND uid = ".$user->uid);
	$items = array();
	while ($row = db_fetch_object($result)) {
		//assigns the nid to the $nid variable
		$nid = $row->nid;
	}
	//set the $vid variable to 3
	$vid = 3;
  //checks if $nid is not null 	(problems begin here)
  if (!is_null($nid)){
	$filter['value'] = array();
	//calls the taxonomy_node_get_terms_by_vocabulary function and assigns the result of this function to the terms variable
	$terms = array_keys(taxonomy_node_get_terms_by_vocabulary($nid, $vid));
	//foreach of the terms returned
	foreach ($terms as $term){
		//stores the tid into the filter['value']  array
		$filter['value'][] = $term->tid;
	}
    //calls the _views_add_taxonomy function
    _views_add_taxonomy($filter['operator'], $filter['value'], $filter['options'], $query);

    $query->where[] = "node.nid != '%s'";
    $query->where_args[] = $nid;
  } // if we've got a node

  // if there is no node, the view shouldn't return anything.
  if (is_null($nid)) {
    $query->where[] = "0";
  } // if there is no node
} // function relatedviews_handler_filter_taxcurnode