fixing bugs in decode
works in basic cases. Still very buggy