Improve rank calculation