Also this whole block could be extracted in a method receiving the input and a config instance, because I guess you'll have twice the same code.