First batch of refactorings. Contributes to #2.