Fixed SQL and refactored some...