added some more checks to the algorithm