some refactoring & bug fixes