Major code cleanup