modified for better performance according to the WWDC talk by Andy Matschuk