added validation and fixed shifting after segment scan