complete decode function