Refactored code and reorganized it