made some minor algoritm adjustments