starting impl of encode