programm shutdown while closing fixed
algorithm made faster