updated algorithm main