completely fixed and implemented algorithm