adding JSON output option; misc. refactoring