refactor and debugging