Refactor, bug fix