Handle case of no results in query
Refactor common code for distinct queries
Move monkey patching to where its needed