Move some functionality to a private helper function to make function a little bit shorter.