Fix #618

@garyb @joneshf I think this is good to go, other than adding a few more tests. Could you please review?
Tests fail unless `main` is exported also.