Refactoring

Reopen of #3