Refactored to a more modular program structure