Fixed stack overflow bug. Seems to be working