the remaining code refactored