several fixes; unit tests; exit code handling