Fixed malloc chrashing problem