code reorganization
misc fix