Refactor encode method from StepsCache to use Encoder class.