code cleanup, reorganization, and formatting