Bug fix in save function

Write to text file moved into the appropriate if statement.