implemented correct sequential algo