implemented decode algorithm