added last part of algorithm DS