Refactored cipher function and brought up to coding/style standards