small refactoring & bug fixes