Refactor custom path handling to avoid too many if conditions