Fix some bugs and improve output