Refactored line intersection function