Refactored code for more modularization