Completed ranking code