Python code tidied up