turn machine rewrite, bug fixes