Refactored the code.

The script runs in two phases now.
1) Collecting issues data (grouped by milestone).
2) Oututting the data into JSON or CSV format.