cleaned up file reading and fixed a mem leak