more robust algorithm to avoid crashes