I have the feeling that this algorithm is not bullet proof, but it works for now.