improved up/down algorithm
refactor