Simplified the code
Added a time out