Fixes to db query