Cleaned up structure of foomail. Removed globals and added functions. Output is now a json object