final merge. some refactoring