Simplified the logic and made it more idiomatic by using the rotate methods that are meant for this.
Made the panic case more explicit. It is still the same panic as before, but now it can be easily removed if we prefer to just ignore invalid arguments.