Small fixes and optimizations for encryption.
Still not tested