Cleaned up code and reordered