some refactoring and cleanups