Cleaned up code and moved some lines