rewritten to remove bugs
Merge pull request #58 from neilsycamore/master

rewritten to remove bugs