refactored to simplify the overall structure