Added comments and some code cleanup.