Fix greedy algorithm