Refactoring

Refactored IN query building to use JSON API rather than array length counting.

Refactored some loops to use the json_array_foreach macro.