Moved encoding, decoding to separate file, added tests.