fixed json output, added comments, refactored