Quentin's algorithm