refactored out some generic code