fixed tofile method to accept filename, kind important
defined $headings for safety