Moved resource allocation into closure