Replaces the iterative approach currently used by an equivalent direct computation. Also fixes infinite looping that happens when the mirroring value is negative.