General refactoring (part 1 of lots)