removed some code duplication