refactor and solved bug