Fixed constant optimizations.

This also fixes the failing tests.