Improved min/max function. 

Better error handling, now max(0, -2px) returns 0 ... etc.