reorganized some of the code