Some mega refactoring