Made the query function a bit more flexible