clean globals; a bit restructuring