Some refactorings to tidy up the mess a bit
Not there yet, but better

$worked 3h on #8