Added comments and did some code cleanup