V3

Moved parts of the code. I need to check the timeout statement. I think
the timeout variable is being rewritten before a timeout can be properly
registered which would defeat the purpose of having it.

Program is hanging in Windows. Need to check if it's working alright in
Ubuntu.