Factoring out globals, work in progress.