near finished script with tally writing function