Many bugxixes to avoid segmentation faults