Extract the lambda functions into separate methods

Extract the multithreading logic in lambda functions into separate
actual functions to make it easier to debug