code cleanup and adding comments