Fix bug in decode and add tests