bug fixes and query improvement