encoding algorithm complete