Split border finder into separate function