Fixed issue 2890 by removing code duplication.