some cleanup and code reorganization