Remove globals, set visibility of functions and variables

Replaced a bunch of globals, set the visibility for all functions and var's.

Change-Id: I9f19d817f4a4a9acf6794fc2706c81a7312ec126