Rework InvalidRank exception

This should fix and close issue #274