General refactoring (work in progress)