some additional refactoring for minor performance improvement and (hopefully) readability