Fix error handling and PhoneGap support