Rank functionality

Partially done, refactoring