Refactored and restructured the code