Code cleanup and internal documentation