Bugfix and reuse old code when the new one isn't used

After you merge this (take the time to review if possible), I think we are ready to merge your PR in Theano.