fixed bug found by mu0n, added unit test.