Refactor code and update debug mode