improved logic

improved logic