improved encoding and decoding