Refactors lineGeneratorForStage and adds unit tests.