added encode method.