General bugfix & removed malloc usage