Added error handling to python and cleaned up