code cleanup and add comments