Large refactoring and close #16