added freeing of resources at end of program, moved a variable
definition