Improve timeout logic