Changed implementation of toQuery so that it can be used
outside of the servlet context