start encoder and some refactoring