refactored long function