refactor all logic