Refactored code. Made it compact