Fix some cases that cause crashes