recursive encoding