added decode function, fixed binary fuctions, fixed encode function