Done some refactoring,changed variable names and removed duplicate code