Refactor using $q.