Refactor

Just a bit of refactoring, this time without touching public interfaces. Tell me if you prefer several pull requests for this.