Cleaned the code and broke it down into functions.