more tolerant algorithm in c/c++ v1.4.2