General code cleanup, preparing for migration to OOP