clean up and some refactoring