Added defensive copy of strings passed to dirname and basename

Added json_decref calls to free memory
Refactored JSON representation of collections and data objects
Permit one-line if statements