fix logic and add test

I got Exception on situation added test.

And made patch to fix it.