Made things a bit more modular. Still need to take care of decrypting.