Re-organizing and debugging runtime seg fault