Change handling of HEAD
Refactoring.

Improve performance.