Fixes #1
lots of refactoring
addition to previous commit
Merge pull request #2 from sheepherd/bugfix

Bugfix