Quite a bit of refactoring (#112).