Fix abnormal termination bug + cosmetics/cleanup