refactored CompareTo method