Fixed issues with database queries