Lots of fixes, made queries functional