fixed some bugs that should improve run time significantlly