Refactoring & bug fix