Fixed very stupid bug. I was using fmax instead of fmin......