fix sleep bug and fix bug in end time with a -1 timeout