General refactoring (part 3 of lots)