Added a timeout if an argument to the wait method is provided.