Applies alternative approach previously suggested by @polarathene and adds test cases to prevent future regressions