Fixed logic error in SQL statement, cleaned up formatting for readability