Finished first encode function