Refactored code to use inheritance instead of copy-paste