refactored and restructured more conservatively