Code optimization and cleanup