extract reused code, improve if statement structure