Fixes #26 Serialize JSON at the end to avoid issues

Refactored the serializing to perform the stringify at the end when all is ready. This fixes issue 26.