Refactor/implement crypto code