small refactoring

Please test this thoroughly before pulling. Especially because of the last commit.