Fixed the aforementioned function (thanks to @vcunat) and rewrote the unit test for it.
This will create a new commit in order to revert the existing changes.