Big refactor. Close #4