The code has been restructured to make sure that all json and curl objects are freed. In addition, some functions have been refactored.