replace hellaciously overcomplicated function with simpler, faster one