Cleanup and working version of the Python script