added completed code for the scan function