Code refactoring

Some cleanup (part 1)