Used os.path functions and string formatting for more robust filename handling
Cleaned up csv writing code
Switched to logging module for output rather than print statements