Algorithm for iOS implementation