Fixed code to address segmentation fault issues on slower machines