Refactored optimizer