refactor, tighten logic