Refactored into more modular code design