factor code from lambda to make readable