added unit tests for encode/decode. Seperated encoding and decoding into methods