Refactored code to use Command design pattern