refactored again to get rid of more code duplication