fix bug on ranks and add unit tests