Fix issue where encrypted keys were being overwritten