Fixing some bugs from previous optimizations.