Refactored code for better abstraction