General code cleanup and documentation