Added check for outputfile existance. Fixed memory leaks.