Refactor code for efficiency and with improved style.