refactored with forward references for improved readability