updated algorithm

updated algorithm