Cleanup extraction function, deal with one additional edge case.
Clean-up the structure of the code.