Fixed three bugs (pointer handling, writing to file and memory leak)