Fix some bugs and append output key for file