removed some code duplication.