Code Refactored and simplified