Added more commments and further cleaned up code