some code reorganized