Removing duplicate code by adding a private function