Cleaned up SQL code