General refactoring (part 2 of lots)