     query: 'loadAppropriateClassification'
 
   componentDidMount: () ->
-    @getCurrentWorkflowID().then (id) =>
-      @getWorkflow @props.project, id
-        .then (workflow) =>
-          @setState
-            workflow: workflow
+    @getCurrentWorkflow().then (workflow) =>
+      @setState {workflow}
 
   loadAppropriateClassification: (_, props = @props) ->
     # To load the right classification, we'll need to know which workflow the user expects.
     # console.log 'Loading appropriate classification'
-    @promiseToSetState classification: @getCurrentWorkflowID(props).then (workflowID) =>
-      # console.log 'Loading classification for workflow', workflowID
+    @promiseToSetState classification: @getCurrentWorkflow(props).then (workflow) =>
+      # console.log 'Loading classification for workflow', workflow.id
       # Create a classification if it doesn't exist for the chosen workflow, then resolve our state with it.
-      currentClassifications.forWorkflow[workflowID] ?= @createNewClassification props.project, workflowID
-      currentClassifications.forWorkflow[workflowID]
-
-  getCurrentWorkflowID: (props = @props) ->
-    getWorkflowID = if props.location.query?.workflow?
+      currentClassifications.forWorkflow[workflow.id] ?= @createNewClassification props.project, workflow
+      currentClassifications.forWorkflow[workflow.id]
+
+  getCurrentWorkflow: (props = @props) ->
+    if props.location.query?.workflow?
       # console.log 'Workflow specified as', props.query.workflow
       # Prefer the workflow specified in the query.
-      Promise.resolve props.location.query.workflow
+      @getWorkflow props.project, props.location.query.workflow
     else
       # If no workflow is specified, pick a random one and record it for later.
       # When we send this classification, we'll clear this value to select a new random workflow.
-      currentWorkflowForProject[props.project.id] ?= @getRandomWorkflowID props.project
+      currentWorkflowForProject[props.project.id] ?= @getRandomWorkflow props.project
       currentWorkflowForProject[props.project.id]
 
-  getRandomWorkflowID: (project) ->
+  getRandomWorkflow: (project) ->
     project.get('workflows', active: true).then (workflows) =>
       if workflows.length is 0
         throw new Error "No workflows for project #{project.id}"

  getWorkflow: (project, workflowID) ->
    # console.log 'Getting workflow', workflowID
    # We could just get the workflow directly, but this way we ensure the workflow belongs to the project.
    project.get('workflows').then (workflows) ->
      workflow = (workflow for workflow in workflows when workflow.id is workflowID)[0]
      unless workflow?
        throw new Error "No workflow #{workflowID} for project #{project.id}"
      workflow

        @setState {projectIsComplete}
        randomIndex = Math.floor Math.random() * workflows.length
        # console.log 'Chose random workflow', workflows[randomIndex].id
        workflows[randomIndex]

  createNewClassification: (project, workflow) ->
    @setState {workflow}
    # A subject set is only specified if the workflow is grouped.
    getSubjectSet = if workflow.grouped
      workflow.get('subject_sets').then (subjectSets) =>
        randomIndex = Math.floor Math.random() * subjectSets.length
        subjectSets[randomIndex]
    else
      Promise.resolve()

    loadSubject = getSubjectSet.then (subjectSet) =>
      @getNextSubject project, workflow, subjectSet

    loadSubject.then (subject) =>
      # console.log 'Creating a new classification'
      classification = apiClient.type('classifications').create
        annotations: []
