implemented greedy step-wise merging