refactored. Fixes #6 and fixes #7