Fixed the segfaults that occured during the tests

Because these are now fixed most of the tests are working correctly.
Some tests still are not working, mostly the foreach related once are
still broken at this point of time.