Refactoring to reduce dependencies and duplicated code (WIP)