small bugs fixed. outputs written to files.