Move fopen outside of calc function