Updated a query to use an sqlite parameter
Refactored some code