What was wrong with the original routine that made you decide you needed to make it 10 times more complicated?  Keep code simple, even if very slightly inefficient, simpler is better.