encoding as per @mledoze