I find it difficult to track what parts of this method are part of the closure and which aren't. Lets hoist this data into a private method and remove some unused imports.