This is a strange algorithm.  Is this a standard way of doing this?