Reorganized some code