Code Refactored and comments added