This is code has about the same performance as the original.