A lot of refactoring. Closed #23