Made the saving a bit more robust and flexible.