added decode, refactored encode