Bunch of bug fixes for encryption