Rearranged main class functions to clean it up