Rewrote rank-checking code, cleaned up