fixed malloc bug and other changes