Updated and merged with changes for iOS compatibility.