fixed stack overflow but several segfaults still exist