debugged min calc