encode/decode and cleanup