Complete refactoring [untested]