Refactor script to be more efficient and handle resuming.