Implement `encode`