Reorganized some of the code