Back-port the iOS7 fixes into the new refactored format (I prefer the multi-file version of this code)