Comments and modularization. Moved some functions to gvars.
Comments added. Move functions to gvars.