Fix build with HEAD
Code cleanup and comments