Made the raycast algorithm more general-purpose