added query class
some bug fixes and improvements