Added several checks and moved some code around to a function.
Added several checks and moved some code around to a function.

PaesslerAG/PythonMiniProbe,MyPhate/PythonMiniProbe,eagle00789/PythonMiniProbe,PaesslerAG/PythonMiniProbe,MyPhate/PythonMiniProbe,eagle00789/PythonMiniProbe