Added checking
Made output to the file