Cleaning up PR #417, added tests and docs

This PR should replace #417