some new test cases and rewrite of code that went lost in one of the last pull requests