Fix file handling and json output.