Fixing a bug and a memory leak