improved logic