Refactoring (almost finish)