Refactored for phone gap