better query code, html template clean up