Broken down main() into several functions to avoid spaghetti code.
Added ignore statement for PyCharm.
Please document things properly!