Modified the code to fit iOS 4