refactor and some logic