Refactor and bug fix
Refactor