use closure and parameters instead of globals
Committing what I have so far.