Reorganized a bunch of code