A lot of small refactoring and some writing