Fixes #133 plus tests.