code cleanup and added additional comments