finished encode functionality