Small fixes to db queries