fixed some bugs and added primitive crash handling