refactor crypt function