Improved compare function.