Changed fclose() to stop seg fault