code cleanup: move code from lambda into separate functon
code cleanup: moved captured locals to parameters