rank function debugged