organizing broker and worker into functions

I was getting familiar with the testcloud code. I cleaned up the code a bit using globals and functions.
Let me know if this goes against any standards we set.