Added comments and restructured code