Added file writing functionality