Updated algorithm according to m4 improvements