Cleaned up code
Added timeout and globals