Added comments and changed the structure of the code.