refactor to fix bug