bug fixes for rank calculation as suggested by Jianfu Chen