some refactor and cleanup