Seems like some of the common code above could be factored out.