Changed to used min function