Fix potential exceptions, close #1815