Bug fix in reference queries