working version of decode in javascript