Extract the last piece splitting to a separated function.

This is only a cosmetic change, but I find the code easier to read after applying it.