code refactoring & bug fix