Refactor

Extract the complex function to a separate class