fixed boundary case infinite loop