Small refactor and bug fix