Extract common file save code