refactored to use $q promises