Lots of tiny refactorings

Thanks @dbaupp