Cleaned up code and moved some sections around