Misc refactoring

A batch of refactoring, from more parts of the code. It needs pretty badly another pair of eyes, 'cause mine are don't see anything anymore.. :crystal_ball: