Fix some bugs around encryption