Tested it and works well. You can omit the !parent.flat condition as @drosca said, since it's always false.