Move globals to an atom and use in a thread-safe manner. Also various formatting modifications.