Updating code

More global -> local variables/functions
Removed unused code.