updated algorithm