FIXED: made min deterministic and more generic