Code refactoring & minor fixes