added some comments for submission and removed duplicate code.