Better raycast algorithm