Move archiving into a class and make more robust