             if c is $scope.task
               parent.children.splice(i, 1)
               break
+
+          # If the deleted task is in the ancestor chain of the active pomodoro, the pomodoro task will be gone.
+          # Detect that by failing to find it in the modified tree, and delete the active pomodoro if it's gone.
+          if $scope.pomodoro? and $scope.pomodoro.active and not findPath($scope.pomodoro.task.id, $scope.rootTask)?
+            $scope.pomodoro = undefined
+
           save()
-          $scope.up()
+          $scope.up($scope.hierarchy.length - 2);
 
     # Saves the editing or newly created button
     $scope.commit = () ->


    # Preorder traverses the task tree, looking for the given ID.
    findTask = (id, task) ->
      if not task?
        task = $scope.rootTask

      if task.id == id
        return task

      if task.children?.length
        for c in task.children
          res = findTask(id, c)
          if res?
            return res

  ]

      storage.fetch 'pomodoro', (value) ->
        if value?
          task = findTask(value.task)
          if task?
            $scope.pomodoro =
              active: true
              type: 'task'
              task: task
              delta: value.delta
              start: Date.now()
              end: Date.now() + value.delta * 60 * 1000
            $scope.$apply()
            startTimer()

    timer = undefined

