refactored optimizer code to make test easier