giant refactor, closes #23