Refactoring path generation code