Improved the encrypt and decrypt functions