Added write and save functions