Major refactor to allow running multiple times a day without corrupting data.