Fix a bug in rank-of, add tests