refactoring of top ca. 85 lines