revised algorithm