Implement encode