Closes #616 and adds unit tests.