Icon cleanups

Various cleanups and refactorings in the icon handling code.  In addition, fix #386 (Untracked icon is not used).