Refactor code for PhoneGap