total refactor. fixes #1, #8, #9, #11