Fixed issues with database queries (again)