Major code cleanup and simplification