Working rotation matching code.