rewrite encode and decode and tests