Fixes #15. Includes all changes from previous commits, working tests and changes requested by evansolomon.