1. bug fixed on IME not initialized for the first time

2. review code for potential nullptr exceptions