cleanup and some refactoring