tidied up so its easy to add these functions to a cordova project