Refactoring for better abstraction and organization