Refactor + bug fix