New attempt at the head-turning algorithm