code reorganization, commenting and cleanup