code cleanup and added some comments