tested with a larger instance. algorithm still correct