Make script PEP8 compliant and separate big main function in many functions.