Refactoring + bug fix