Fix global variable usage and cleanup whitespace