First draft of cleaning up globals and protecting header consistency