Added comments and moved most of the code into functions.