Fixed file only opening once in function