Revert #39 and fix #41.

Also closes #40.