Refactoring, bug fix