Fixed encode function and added decode function