Refactoring internals

See #167