better error handling and minor cleanup
Merge pull request #39 from dimagi/clean

better error handling and minor cleanup