Fix some bugs. Shouldn't be any crashes any more.