Fix bugs exposed by theano

I also added the option of keeping kernels files around when compiled in debug mode.