complete refactor, fixes #1