Added comments and performed some minor refactoring.